//
//  NYAcccountViewModel.m
//  Nooryes
//
//  Created by baken on 2020/2/28.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYAcccountViewModel.h"
#import "NYAccountManager.h"
#import "NYAccountModel.h"
#import "NYArticleListModel.h"
#import "NYTopicListModel.h"
#import "AccountModel.h"
#import "NYBindListModel.h"
#import "NYMessageListModel.h"
#import "NYMainModel.h"

@implementation NYAcccountViewModel

- (void)getUserInfoWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYAccountManager getUserInfoWithUserID:uid andSessionID:sessionid andCompletionHandle:^(NYAccountModel * model, NSError * error) {
        if (!error) {
            myDelegate.account = model.data;
            self.account = model.data;
        }
        complete(error);
    }];
}


- (void)getArticleCollectListWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYAccountManager getArticleCollectListWithUserID:uid andSessionID:sessionid andType:@"1" andPage:STR_FROM_INT(self.articlePage) andCompletionHandle:^(NYArticleListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            if (self.articlePage == 1) {
                [self.articalArray removeAllObjects];
            }
            [self.articalArray addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}


- (void)getTopicCollectListWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYAccountManager getTopicCollectListWithUserID:uid andSessionID:sessionid andType:@"2" andPage:STR_FROM_INT(self.topicPage) andCompletionHandle:^(NYTopicListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            if (self.topicPage == 1) {
                [self.topicArray removeAllObjects];
            }
            [self.topicArray addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}

- (void)getMessageListWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYAccountManager getMessageListWithUserID:uid andSessionID:sessionid andPage:@"" andCompletionHandle:^(NYMessageListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            [self.msgArray addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}

- (void)getMessageCountWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYAccountManager getMessageCountWithUserID:uid andSessionID:sessionid andCompletionHandle:^(NYMainModel * _Nonnull model, NSError * _Nonnull error) {
        NSString *count = [[NSUserDefaults standardUserDefaults] objectForKey:@"msgCount"];
        if (![count isEqualToString:model.data]) {
            self.msgCount = model.data;
        }
        
        complete(error);
    }];
}

-(NSMutableArray *)topicArray{
    if (!_topicArray) {
        _topicArray = [NSMutableArray array];
    }
    return _topicArray;
}  

-(NSMutableArray *)articalArray{
    if (!_articalArray) {
        _articalArray= [NSMutableArray array];
    }
    return _articalArray;
}

-(NSMutableArray *)msgArray{
    if (!_msgArray) {
        _msgArray= [NSMutableArray array];
    }
    return _msgArray;
}

@end
